namespace CookieTerm.MdiTabStrip
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Windows.Forms.Design;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Reflection;
    using System.Windows.Forms;
    using System.Drawing;

    public class MdiTabStripDesigner : ControlDesigner
    {


        private DesignerActionListCollection m_actionLists = null;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (this.m_actionLists == null)
                {
                    this.m_actionLists = new DesignerActionListCollection();
                    this.m_actionLists.Add(new MdiTabStripDesignerActionList(this.Component));
                }

                return this.m_actionLists;
            }
        }

        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            MdiTabStrip tabStrip = (MdiTabStrip)Control;
            MdiTab activeTab = new MdiTab(tabStrip);
            MdiTab inactiveTab = new MdiTab(tabStrip);
            MdiTab mouseOverTab = new MdiTab(tabStrip);

            tabStrip.LeftScrollTab.Visible = true;
            tabStrip.RightScrollTab.Visible = true;
            tabStrip.DropDownTab.Visible = true;

            activeTab.Form = new Form1();
            tabStrip.ActiveTab = activeTab;
            tabStrip.Tabs.Add(activeTab);

            inactiveTab.Form = new Form2();
            tabStrip.Tabs.Add(inactiveTab);

            mouseOverTab.Form = new Form3();
            mouseOverTab.IsMouseOver = true;
            tabStrip.Tabs.Add(mouseOverTab);

            tabStrip.PerformLayout();
        }

        private class Form1 : Form
        {

            public Form1()
            {
                this.Text = "Active Tab";
                this.Icon = Resources.Document;
            }
        }

        private class Form2 : Form
        {

            public Form2()
            {
                this.Text = "Inactive Tab";
                this.Icon = Resources.Document;
            }
        }

        private class Form3 : Form
        {

            public Form3()
            {
                this.Text = "Moused Over Tab";
                this.Icon = Resources.Document;
            }
        }
    }

    public class MdiTabStripDesignerActionList : DesignerActionList
    {

        private DesignerActionUIService _uiService = null;

        private DesignerActionItemCollection _actionItems = null;
        public MdiTabStripDesignerActionList(IComponent component)
            : base(component)
        {
            this._uiService = (DesignerActionUIService)GetService(typeof(DesignerActionUIService));
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            if (_actionItems == null)
            {
                _actionItems = new DesignerActionItemCollection();

                if (TabStrip != null)
                {
                    _actionItems.Add(new DesignerActionMethodItem(this, "OpenInactiveTabEditor", "Design Tabs", "Appearance", "Opens the MdiTab Designer window."));

                    _actionItems.Add(new DesignerActionHeaderItem("Behavior"));
                    _actionItems.Add(new DesignerActionPropertyItem("TabPermanence", "Tab permanence", GetCategory(this.TabStrip, "TabPermanence"), GetDescription(this.TabStrip, "TabPermanence")));
                    _actionItems.Add(new DesignerActionPropertyItem("Animate", "Perform fade animation on mouse over", GetCategory(this.TabStrip, "Animate"), GetDescription(this.TabStrip, "Animate")));
                    _actionItems.Add(new DesignerActionPropertyItem("DisplayFormIcon", "Display the form icon", "Behavior", GetDescription(this.TabStrip, "DisplayFormIcon")));
                    _actionItems.Add(new DesignerActionPropertyItem("MdiNewTabVisible", "Display the new tab", "Behavior", GetDescription(this.TabStrip, "MdiNewTabVisible")));

                    _actionItems.Add(new DesignerActionHeaderItem("Layout"));
                    _actionItems.Add(new DesignerActionPropertyItem("MinTabWidth", "Minimum tab width", GetCategory(this.TabStrip, "MinTabWidth"), GetDescription(this.TabStrip, "MinTabWidth")));
                    _actionItems.Add(new DesignerActionPropertyItem("MaxTabWidth", "Maximum tab width", GetCategory(this.TabStrip, "MaxTabWidth"), GetDescription(this.TabStrip, "MaxTabWidth")));
                    _actionItems.Add(new DesignerActionPropertyItem("MdiWindowState", "Mdi form window state", GetCategory(this.TabStrip, "MdiWindowState"), GetDescription(this.TabStrip, "MdiWindowState")));
                }
            }

            return _actionItems;
        }

        public MdiTabStrip TabStrip
        {
            get { return (MdiTabStrip)base.Component; }
        }

        private void SetProperty(string propertyName, object value)
        {
            PropertyDescriptor prop = TypeDescriptor.GetProperties(this.TabStrip)[propertyName];
            prop.SetValue(this.TabStrip, value);
        }

        private string GetCategory(object source, string propertyName)
        {
            PropertyInfo prop = source.GetType().GetProperty(propertyName);
            object[] attributes = prop.GetCustomAttributes(typeof(CategoryAttribute), false);

            if (attributes.Length == 0)
            {
                return null;
            }

            CategoryAttribute attr = (CategoryAttribute)attributes[0];

            if (attr == null)
            {
                return null;
            }

            return attr.Category;
        }

        private string GetDescription(object source, string propertyName)
        {
            PropertyInfo prop = source.GetType().GetProperty(propertyName);
            object[] attributes = prop.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes.Length == 0)
            {
                return null;
            }

            DescriptionAttribute attr = (DescriptionAttribute)attributes[0];

            if (attr == null)
            {
                return null;
            }

            return attr.Description;
        }

        #region "MdiTabStrip Properties"
        public Color ActiveTabColor
        {
            get { return this.TabStrip.ActiveTabColor; }
            set { SetProperty("ActiveTabColor", value); }
        }

        public Color ActiveTabForeColor
        {
            get { return this.TabStrip.ActiveTabForeColor; }
            set { SetProperty("ActiveTabForeColor", value); }
        }

        public Font ActiveTabFont
        {
            get { return this.TabStrip.ActiveTabFont; }
            set { SetProperty("ActiveTabFont", value); }
        }

        public Color CloseButtonBackColor
        {
            get { return this.TabStrip.CloseButtonBackColor; }
            set { SetProperty("CloseButtonBackColor", value); }
        }

        public Color CloseButtonForeColor
        {
            get { return this.TabStrip.CloseButtonForeColor; }
            set { SetProperty("CloseButtonForeColor", value); }
        }

        public Color CloseButtonHotForeColor
        {
            get { return this.TabStrip.CloseButtonHotForeColor; }
            set { SetProperty("CloseButtonHotForeColor", value); }
        }

        public Color CloseButtonBorderColor
        {
            get { return this.TabStrip.CloseButtonBorderColor; }
            set { SetProperty("CloseButtonBorderColor", value); }
        }

        public Color InactiveTabColor
        {
            get { return this.TabStrip.InactiveTabColor; }
            set { SetProperty("InactiveTabColor", value); }
        }

        public Color InactiveTabForeColor
        {
            get { return this.TabStrip.InactiveTabForeColor; }
            set { SetProperty("InactiveTabForeColor", value); }
        }

        public Font InactiveTabFont
        {
            get { return this.TabStrip.InactiveTabFont; }
            set { SetProperty("InactiveTabFont", value); }
        }

        public Color MouseOverTabColor
        {
            get { return this.TabStrip.MouseOverTabColor; }
            set { SetProperty("MouseOverTabColor", value); }
        }

        public Color MouseOverTabForeColor
        {
            get { return this.TabStrip.MouseOverTabForeColor; }
            set { SetProperty("MouseOverTabForeColor", value); }
        }

        public Font MouseOverTabFont
        {
            get { return this.TabStrip.MouseOverTabFont; }
            set { SetProperty("MouseOverTabFont", value); }
        }

        public Color ActiveTabBorderColor
        {
            get { return this.TabStrip.ActiveTabBorderColor; }
            set { SetProperty("ActiveTabBorderColor", value); }
        }

        public Color InactiveTabBorderColor
        {
            get { return this.TabStrip.InactiveTabBorderColor; }
            set { SetProperty("InactiveTabBorderColor", value); }
        }

        public bool Animate
        {
            get { return this.TabStrip.Animate; }
            set { SetProperty("Animate", value); }
        }

        public MdiTabPermanence TabPermanence
        {
            get { return this.TabStrip.TabPermanence; }
            set { SetProperty("TabPermanence", value); }
        }

        public int MaxTabWidth
        {
            get { return this.TabStrip.MaxTabWidth; }
            set { SetProperty("MaxTabWidth", value); }
        }

        public int MinTabWidth
        {
            get { return this.TabStrip.MinTabWidth; }
            set { SetProperty("MinTabWidth", value); }
        }

        public bool DisplayFormIcon
        {
            get { return this.TabStrip.DisplayFormIcon; }
            set { SetProperty("DisplayFormIcon", value); }
        }

        public MdiChildWindowState MdiWindowState
        {
            get { return this.TabStrip.MdiWindowState; }
            set { SetProperty("MdiWindowState", value); }
        }

        public RightToLeft RightToLeft
        {
            get { return this.TabStrip.RightToLeft; }
        }

        public bool MdiNewTabVisible
        {
            get { return this.TabStrip.MdiNewTabVisible; }
            set { SetProperty("MdiNewTabVisible", value); }
        }
        #endregion

        private void OpenInactiveTabEditor()
        {
            MdiTabStripDesignerForm editor = new MdiTabStripDesignerForm();
            MdiTabTemplateControl template = new MdiTabTemplateControl();

            template.InactiveTabTemplate.BackColor = this.InactiveTabColor;
            template.InactiveTabTemplate.ForeColor = this.InactiveTabForeColor;
            template.InactiveTabTemplate.Font = this.InactiveTabFont;
            template.InactiveTabTemplate.BorderColor = this.InactiveTabBorderColor;
            template.ActiveTabTemplate.BackColor = this.ActiveTabColor;
            template.ActiveTabTemplate.ForeColor = this.ActiveTabForeColor;
            template.ActiveTabTemplate.Font = this.ActiveTabFont;
            template.ActiveTabTemplate.BorderColor = this.ActiveTabBorderColor;
            template.ActiveTabTemplate.CloseButtonBackColor = this.CloseButtonBackColor;
            template.ActiveTabTemplate.CloseButtonBorderColor = this.CloseButtonBorderColor;
            template.ActiveTabTemplate.CloseButtonForeColor = this.CloseButtonForeColor;
            template.ActiveTabTemplate.CloseButtonHotForeColor = this.CloseButtonHotForeColor;
            template.MouseOverTabTemplate.BackColor = this.MouseOverTabColor;
            template.MouseOverTabTemplate.ForeColor = this.MouseOverTabForeColor;
            template.MouseOverTabTemplate.Font = this.MouseOverTabFont;
            template.RightToLeft = this.RightToLeft;

            editor.TabTemplate = template;
            editor.ShowDialog();

            if (editor.DialogResult == DialogResult.OK)
            {
                this.InactiveTabColor = editor.TabTemplate.InactiveTabTemplate.BackColor;
                this.InactiveTabForeColor = editor.TabTemplate.InactiveTabTemplate.ForeColor;
                this.InactiveTabFont = editor.TabTemplate.InactiveTabTemplate.Font;
                this.InactiveTabBorderColor = editor.TabTemplate.InactiveTabTemplate.BorderColor;
                this.ActiveTabColor = editor.TabTemplate.ActiveTabTemplate.BackColor;
                this.ActiveTabForeColor = editor.TabTemplate.ActiveTabTemplate.ForeColor;
                this.ActiveTabBorderColor = editor.TabTemplate.ActiveTabTemplate.BorderColor;
                this.ActiveTabFont = editor.TabTemplate.ActiveTabTemplate.Font;
                this.CloseButtonBackColor = editor.TabTemplate.ActiveTabTemplate.CloseButtonBackColor;
                this.CloseButtonForeColor = editor.TabTemplate.ActiveTabTemplate.CloseButtonForeColor;
                this.CloseButtonHotForeColor = editor.TabTemplate.ActiveTabTemplate.CloseButtonHotForeColor;
                this.CloseButtonBorderColor = editor.TabTemplate.ActiveTabTemplate.CloseButtonBorderColor;
                this.MouseOverTabColor = editor.TabTemplate.MouseOverTabTemplate.BackColor;
                this.MouseOverTabForeColor = editor.TabTemplate.MouseOverTabTemplate.ForeColor;
                this.MouseOverTabFont = editor.TabTemplate.MouseOverTabTemplate.Font;
            }
        }
    }

    //=======================================================
    //Service provided by Telerik (www.telerik.com)
    //Conversion powered by NRefactory.
    //Twitter: @telerik
    //Facebook: facebook.com/telerik
    //=======================================================
}